package com.app.service.impl;

import com.app.mapper.DutiesMapper;
import com.app.model.DutiesModel;
import com.app.model.SystemModel;
import com.app.service.DutiesService;
import com.app.service.SupervisionService;
import com.app.vo.SupervisionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DutiesServiceImpl implements DutiesService {
    @Autowired
    private DutiesMapper dutiesMapper;
    @Autowired
    private SupervisionService supervisionService;

    @Override
    public void insert(DutiesModel dutiesModel) {
        dutiesMapper.insert(dutiesModel);
    }

    @Override
    public void update(DutiesModel dutiesModel) {
        dutiesMapper.updateById(dutiesModel);
    }

    @Override
    public void deleteDutiesById(int id) {
        dutiesMapper.deleteById(id);
    }

    @Override
    public SupervisionVo selectDuties(Integer page, Integer limit, String keyword, Integer type) {
        List<SystemModel> systemModels = supervisionService.selectType();
        page = (page - 1) * limit;
        if (type == 0) {
            List<DutiesModel> dutiesModelList = dutiesMapper.selectAllDuties(page, limit, keyword);
            for (int i = 0; i < dutiesModelList.size(); i++) {
                for (int i1 = 0; i1 < systemModels.size(); i1++) {
                    if (dutiesModelList.get(i).getPid() != null && dutiesModelList.get(i).getPid() == systemModels.get(i1).getId()) {
                        dutiesModelList.get(i).setPidText(systemModels.get(i1).getName());
                    }
                }
            }
            Integer count = dutiesMapper.getCount();
            SupervisionVo vo = new SupervisionVo();
            vo.setList(dutiesModelList);
            vo.setCount(count);
            return vo;
        }
        List<DutiesModel> dutiesModelList = dutiesMapper.selectDuties(page, limit, keyword, type);
        for (int i = 0; i < dutiesModelList.size(); i++) {
            for (int i1 = 0; i1 < systemModels.size(); i1++) {
                if (dutiesModelList.get(i).getPid() != null && dutiesModelList.get(i).getPid() == systemModels.get(i1).getId()) {
                    dutiesModelList.get(i).setPidText(systemModels.get(i1).getName());
                }
            }
        }
        Integer count = dutiesMapper.getCountByType(type);
        SupervisionVo vo = new SupervisionVo();
        vo.setList(dutiesModelList);
        vo.setCount(count);
        return vo;
    }
}
